﻿using JunQuanPrinter.DB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace JunQuanPrinter
{
    public partial class frmLoadFile : Form
    {
        private static frmLoadFile m_frm = null;
        private CDataParam m_DataParamDB = CDataParam.getClass();
        public static frmLoadFile getClass()
        {
            if(m_frm == null)
            {
                m_frm = new frmLoadFile();
            }
            return m_frm;
        }

        public frmLoadFile()
        {
            InitializeComponent();
        }

        public static void CloseForm()
        {
            m_frm = null;
        }

        private void btnSelFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Excel 文件|*.xlsx|Excel 文件|*.xls";
            if(dlg.ShowDialog() == DialogResult.OK)
            {
                edtFileName.Text = dlg.FileName;
            }
        }


        

        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                String strExcelFile = edtFileName.Text.Trim();
                TDataParam Data = new TDataParam();
                DataTable dt = CEpplus.WorksheetToTable(strExcelFile);
                Int32 nRowIdx = 0;
                progressBarDown.Maximum = dt.Rows.Count - 1;
                progressBarDown.Value = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    
                    if ((nRowIdx++) == 0)
                        continue;

                    Data.PRODUCT_CODE = Convert.ToString(dr["1"]);
                    Data.LINE_NUM = Convert.ToString(dr["2"]);
                    Data.SPLIT = Convert.ToString(dr["3"]);
                    Data.PRINT_SPEED = Convert.ToString(dr["4"]);
                    Data.TIME_DELAY = Convert.ToString(dr["5"]);
                    Data.WORD_A = Convert.ToString(dr["6"]);
                    Data.Font = Convert.ToString(dr["7"]);
                    Data.FontSize = Convert.ToString(dr["8"]);

                    m_DataParamDB.AddData(Data);
                    progressBarDown.Value++;
                    Application.DoEvents();
                }                

                MessageBox.Show("导入数据成功！");
            }
            catch(Exception ex)
            {
                MessageBox.Show("导入Excel文件失败," + ex.Message);
            }
            
        }
    }
}
